<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC
    "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
    
<sqlMap namespace="${beanName}">
  <resultMap class="${className}" id="${beanName}">
<#list properties as p>
    <result property="${p.propertyName}" column="${p.columnName}" />
</#list>
  </resultMap>
  
  <sql id="masterStatement">
   <![CDATA[SELECT * FROM ${tableName} ]]>
  </sql>

  <sql id="findWhereStatement">
<#list properties as p>
<#if p.propertyTypeName == "java.lang.String">
      <isNotEmpty prepend="AND" property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotEmpty>
<#else>
      <isNotNull prepend="AND" property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotNull>
</#if>
</#list>
  </sql>
  
  <select id="getCount" parameterClass="${className}" resultClass="java.lang.Integer">
    <![CDATA[
    SELECT COUNT(*) CNT FROM ${tableName} 
    ]]>
    <dynamic prepend="WHERE">
      <include refid="findWhereStatement" />
    </dynamic>
  </select>
  
  <select id="find${simpleClassName}"
    parameterClass="${className}" resultMap="${beanName}">
    <include refid="masterStatement" />
    <dynamic prepend="WHERE">
      <include refid="findWhereStatement" />
    </dynamic>
  </select>

  <select id="find${simpleClassName}ByPage" parameterClass="java.util.HashMap" resultMap="${beanName}">
    <include refid="ORACLE.paginationStart" />
    <include refid="masterStatement" />
    <dynamic prepend="WHERE">
      <include refid="findWhereStatement" />
    </dynamic>
    <include refid="ORACLE.paginationEnd" />
  </select>

  <select id="get${simpleClassName}ById" parameterClass="java.lang.Long" resultMap="${beanName}">
    <include refid="masterStatement" />
    <![CDATA[
       WHERE ID=#value#
     ]]>
  </select>

  <insert id="insert${simpleClassName}" parameterClass="${className}">
   <selectKey keyProperty="id" resultClass="java.lang.Long">
   <![CDATA[
       SELECT S_${tableName}.NEXTVAL FROM DUAL
   ]]>
   </selectKey>
      INSERT INTO ${tableName} 
      <dynamic prepend="(" >
<#list properties as p>
<#if p.propertyTypeName == "java.lang.String">
        <isNotEmpty prepend="," property="${p.propertyName}" > ${p.columnName} </isNotEmpty>
<#else>
        <isNotNull prepend="," property="${p.propertyName}" > ${p.columnName} </isNotNull>
</#if>
</#list>
      )
      </dynamic>
    VALUES 
      <dynamic prepend="(" >
<#list properties as p>
<#if p.propertyTypeName == "java.lang.String">
        <isNotEmpty prepend="," property="${p.propertyName}" > #${p.propertyName}# </isNotEmpty>
<#else>
        <isNotNull prepend="," property="${p.propertyName}" > #${p.propertyName}# </isNotNull>
</#if>
</#list>
      )
      </dynamic>
  </insert>
  
  <update id="update${simpleClassName}ById" parameterClass="${className}">
  <![CDATA[
      UPDATE ${tableName}
  ]]>
  <dynamic prepend="SET">
<#list properties as p>
    <isNotNull prepend="," property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotNull>
</#list>
  </dynamic>
  <![CDATA[
      WHERE ID=#id#
  ]]>
  </update>

</sqlMap>